
package com.hyst.seviceImpl;

import com.hyst.common.ResultBean;
import com.hyst.domain.Teacher;
import com.hyst.domain.TeacherPeriod;
import com.hyst.repository.TeacherMapper;
import com.hyst.service.TeacherPeriodService;
import com.hyst.service.TeacherService;
import io.swagger.models.auth.In;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.Period;
import com.hyst.repository.PeriodMapper;
import com.hyst.service.PeriodService;
import com.hyst.utils.UuidTool;

import java.util.*;

/**
 * @version ：1.0 1.[2018年08月25日]
 */
@Service
public class PeriodServiceImpl implements PeriodService {

    @Autowired
    private PeriodMapper mapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeacherPeriodService teacherPeriodService;

    @Override
    public Page<Period> list(final Period period) {
        Page<Period> page = PageHelper.startPage(period.getPageNum(), period.getPageSize());
        mapper.select(period);
        return page;
//        return list2(period,"F40249E1BF4A4306898696CE2EB1823C");
    }

    public ResultBean list2(String teacherId) {
        try {
            TeacherPeriod teacherPeriod = new TeacherPeriod();
            teacherPeriod.setTeacherid(teacherId);
            List<List<TeacherPeriod>> TeacherPeriods = teacherPeriodService.teacherPeriods(teacherPeriod).getData();
            Set<Integer> times = new HashSet<>();
            for (List<TeacherPeriod> pds : TeacherPeriods) {
                for (TeacherPeriod tp : pds) {
                    times.add(tp.getTimeid());
                }
            }

            Set<Integer> result = new HashSet<>();
            Period period = new Period();
            List<Period> select = mapper.select(period);
            for (Period period1 : select) {
                if(!times.contains(period1.getId())){
                    result.add(period1.getId());
                }
            }
            return ResultBean.success(new ArrayList<Integer>(result));
//
//            Page<Period> page = PageHelper.startPage(period.getPageNum(), period.getPageSize());
//            if (times.size() > 0) {
//                mapper.getPeriods(new ArrayList<Integer>(times));
//            } else {
//                mapper.select(period);
//            }
//            return page;
        }catch (Exception e){
            e.printStackTrace();
        }
       return null;
    }

    @Override
    public  Page<Period> getPeriodsUsed(final String teacherId) {
        Period period = new Period();
        period.setPageNum(1);
        period.setPageSize(200);
        Page<Period> page = PageHelper.startPage(1, 200);

        //时间表
        List<Period> periodtable = mapper.select(period);

        //教师
        TeacherPeriod teacherPeriod = new TeacherPeriod();
        teacherPeriod.setTeacherid(teacherId);
        Page<TeacherPeriod> list = teacherPeriodService.list(teacherPeriod);
        List<TeacherPeriod> result = list.getResult();

        Map<Integer, List<TeacherPeriod>> periodMap = new HashMap<>();

        for (TeacherPeriod t : result) {
            Integer timeid = t.getTimeid();
            String useMsg = t.getUseMsg();
            if ("可用时段".equals(useMsg)) {
                if (periodMap.containsKey(timeid)) {
                    periodMap.get(timeid).add(t);
                } else {
                    List<TeacherPeriod> l = new ArrayList<>();
                    l.add(t);
                    periodMap.put(timeid, l);
                }
            }
        }

        // 时间处理
        for (Period p : periodtable) {
            Integer timeId = p.getId();
            if (periodMap.containsKey(timeId) && !periodMap.get(timeId).isEmpty()) {
                List<TeacherPeriod> teacherPeriods = periodMap.get(timeId);
                for (TeacherPeriod tp : teacherPeriods) {
                    String week = tp.getWeek();
                    weedhandle(p, week);
                }
            }
        }
        return page;
    }

    private void weedhandle(Period p, String week) {
        switch (week) {
            case "1":
                p.setMon(p.getMon() + 1);
                return;
            case "2":
                p.setTue(p.getTue() + 1);
                return;
            case "3":
                p.setWed(p.getWed() + 1);
                return;
            case "4":
                p.setThu(p.getThu() + 1);
                return;
            case "5":
                p.setFri(p.getFri() + 1);
                return;
            case "6":
                p.setSat(p.getSat() + 1);
                return;
            case "7":
                p.setSun(p.getSun() + 1);
                return;
        }

    }

    enum WeekNum {
        MON("mon", "1"),
        TUE("tue", "2"),
        WED("wed", "3"),
        THU("thu", "4"),
        FRI("fir", "5"),
        SAT("sat", "6"),
        SUN("sun", "7");

        WeekNum(String str, String num) {
            this.str = str;
            this.num = num;
        }

        private String str;

        private String num;

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        public String getNum() {
            return num;
        }

        public void setNum(String num) {
            this.num = num;
        }
    }

    @Override
    public Period insert(final Period period) {
        mapper.insertSelective(period);
        return period;
    }

    @Override
    public Period getByPriKey(final Integer id) {
        final Period period = mapper.selectByPrimaryKey(id);
        return period;
    }

    @Override
    public Period updateSelective(final Period period) {
        mapper.updateByPrimaryKeySelective(period);
        return period;
    }

    @Override
    public Period update(final Period period) {
        mapper.updateByPrimaryKey(period);
        return period;
    }

    @Override
    public Period delete(final Integer id) {

        return null;
    }

}
